package com.topscomm.core.factory.minio;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import com.google.common.collect.Multimap;

import io.minio.CreateMultipartUploadResponse;
import io.minio.ListPartsResponse;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.messages.Part;

/**
 * 自定义minioClient客户端，实现自定义分片上传的支持
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月18日下午1:51:11
 * @modify:
 */
public class MinioFileClient extends MinioClient {

	protected MinioFileClient(MinioClient client) {
		super(client);
	}

	/**
	 * 初始化分片文件上传的uploadId
	 * 
	 * @description:
	 * @param bucket
	 * @param region
	 * @param object
	 * @param headers
	 * @param extraQueryParams
	 * @return
	 * @throws IOException
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InsufficientDataException
	 * @throws ServerException
	 * @throws InternalException
	 * @throws XmlParserException
	 * @throws InvalidResponseException
	 * @throws ErrorResponseException
	 * @author: donghang
	 * @date: 2021年3月18日下午1:52:02
	 * @modify:
	 */
	public String initMultiPartUpload(String bucket, String region, String object, Multimap<String, String> headers,
			Multimap<String, String> extraQueryParams)
			throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException,
			ServerException, InternalException, XmlParserException, InvalidResponseException, ErrorResponseException {
		CreateMultipartUploadResponse response = this.createMultipartUpload(bucket, region, object, headers,
				extraQueryParams);

		return response.result().uploadId();
	}

	/**
	 * 合并分片文件
	 * 
	 * @description:
	 * @param bucketName
	 * @param region
	 * @param objectName
	 * @param uploadId
	 * @param parts
	 * @param extraHeaders
	 * @param extraQueryParams
	 * @return
	 * @throws IOException
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InsufficientDataException
	 * @throws ServerException
	 * @throws InternalException
	 * @throws XmlParserException
	 * @throws InvalidResponseException
	 * @throws ErrorResponseException
	 * @author: donghang
	 * @date: 2021年3月18日下午1:56:46
	 * @modify:
	 */
	public ObjectWriteResponse mergeMultipartUpload(String bucketName, String region, String objectName,
			String uploadId, Part[] parts, Multimap<String, String> extraHeaders,
			Multimap<String, String> extraQueryParams)
			throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException,
			ServerException, InternalException, XmlParserException, InvalidResponseException, ErrorResponseException {

		return this.completeMultipartUpload(bucketName, region, objectName, uploadId, parts, extraHeaders,
				extraQueryParams);
	}

	/**
	 * 
	 * @description:
	 * @param bucketName
	 * @param region
	 * @param objectName
	 * @param maxParts
	 * @param partNumberMarker
	 * @param uploadId
	 * @param extraHeaders
	 * @param extraQueryParams
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InsufficientDataException
	 * @throws IOException
	 * @throws InvalidKeyException
	 * @throws ServerException
	 * @throws XmlParserException
	 * @throws ErrorResponseException
	 * @throws InternalException
	 * @throws InvalidResponseException
	 * @author: donghang
	 * @date: 2021年3月18日下午1:57:07
	 * @modify:
	 */
	public ListPartsResponse listMultipart(String bucketName, String region, String objectName, Integer maxParts,
			Integer partNumberMarker, String uploadId, Multimap<String, String> extraHeaders,
			Multimap<String, String> extraQueryParams)
			throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException,
			ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
		return this.listParts(bucketName, region, objectName, maxParts, partNumberMarker, uploadId, extraHeaders,
				extraQueryParams);
	}

}
